<?php
namespace app\admin\controller\agent;
use app\admin\controller\Controller;
use app\admin\model\agent\User as UserModel;
use app\admin\model\agent\Agent as AgentModel;
use think\Db;
use think\Session;
/**
 * 人员管理
 */

class Agent extends Controller{
    protected  $model;
    public function _initialize(){
        parent::_initialize();
        $this->model=new AgentModel();
    }

    /**
     * 人员列表
     */
    public function index(){
        $userinfo=Db::name('love_admin_user')->where(['id'=>Session::get('yoshop_admin')['user']['id']])->find();
        $title=input('get.name');
        if($title){
            $where['name|user_name']=["like",'%'.trim($title).'%'];
        }
        if($userinfo['type']==1){
            //主管理员
            $where['id']=['<>',0];
        }elseif ($userinfo['type']==2){
            //城市管理员
            $where['cityid']=['=',$userinfo['cityid']];
            $where['type']=['in','3,4,5'];
        }else{
            //区域管理员
            $where['areaid']=['=',$userinfo['areaid']];
            $where['type']=['in','4,5'];
        }
        $list=Db::name('love_admin_user')->where($where)->order('create_time desc')->paginate(15, false, [
            'query' => \request()->request()
        ]);
        return $this->fetch('index', [
            'list' =>$list,
        ]);
    }
    public function x(){
        //获取所有城市管理员
        $roleList=Db::name('love_area')->where(['pid'=>0,'status'=>1])->field('id,name,pid')->select()->toArray();
        //获取城市,区域,楼盘三级联动
        $newArray=[];
        foreach ($roleList as $k=>$v){
            $city=Db::name('love_area')->where(['pid'=>$v['id']])->field('id,name,pid')->select()->toArray();
            $newArray[$v['id']]=$v;
            $newArray[$v['id']]['level']=1;
            foreach ($city as $k1=>$v1){
                $newArray[$v['id']]['city'][$v1['id']]=$v1;
                $newArray[$v['id']]['city'][$v1['id']]['level']=2;
                $newArray[$v['id']]['city'][$v1['id']]['region']=[];
                $region=Db::name('love_building')->where(['areaId'=>$v1['id'],'guanlisel'=>0])->field('id,name')->select()->toArray();
                foreach ($region as $k2=>$v2){
                    $newArray[$v['id']]['city'][$v1['id']]['region'][$v2['id']]=$v2;
                    $newArray[$v['id']]['city'][$v1['id']]['region'][$v2['id']]['pid']=$v1['id'];
                    $newArray[$v['id']]['city'][$v1['id']]['region'][$v2['id']]['level']=3;
                }
            }
        }
        return json(['code'=>'success','data'=>$newArray]);
    }
    public function y(){
        //获取所有城市管理员
        $roleList=Db::name('love_area')->where(['pid'=>0,'status'=>1])->field('id,name,pid')->select()->toArray();
        //获取城市,区域,楼盘三级联动
        $newArray=[];
        foreach ($roleList as $k=>$v){
            $city=Db::name('love_area')->where(['pid'=>$v['id']])->field('id,name,pid')->select()->toArray();
            $newArray[$v['id']]=$v;
            $newArray[$v['id']]['level']=1;
            foreach ($city as $k1=>$v1){
                $newArray[$v['id']]['city'][$v1['id']]=$v1;
                $newArray[$v['id']]['city'][$v1['id']]['level']=2;
                $newArray[$v['id']]['city'][$v1['id']]['region']=[];
                $region=Db::name('love_building')->where(['areaId'=>$v1['id']])->field('id,name')->select()->toArray();
                foreach ($region as $k2=>$v2){
                    $newArray[$v['id']]['city'][$v1['id']]['region'][$v2['id']]=$v2;
                    $newArray[$v['id']]['city'][$v1['id']]['region'][$v2['id']]['pid']=$v1['id'];
                    $newArray[$v['id']]['city'][$v1['id']]['region'][$v2['id']]['level']=3;
                }
            }
        }
        return json(['code'=>'success','data'=>$newArray]);
    }
    public function z(){
        //获取所有城市管理员
        $roleList=Db::name('love_area')->where(['pid'=>0,'status'=>1])->field('id,name,pid')->select()->toArray();
        //获取城市,区域,楼盘三级联动
        $newArray=[];
        foreach ($roleList as $k=>$v){
            $city=Db::name('love_area')->where(['pid'=>$v['id']])->field('id,name,pid')->select()->toArray();
            $newArray[$v['id']]=$v;
            $newArray[$v['id']]['level']=1;
            foreach ($city as $k1=>$v1){
                $newArray[$v['id']]['city'][$v1['id']]=$v1;
                $newArray[$v['id']]['city'][$v1['id']]['level']=2;
                $newArray[$v['id']]['city'][$v1['id']]['region']=[];
                $region=Db::name('love_building')->where(['areaId'=>$v1['id']])->field('id,name')->select()->toArray();
                foreach ($region as $k2=>$v2){
                    $newArray[$v['id']]['city'][$v1['id']]['region'][$v2['id']]=$v2;
                    $newArray[$v['id']]['city'][$v1['id']]['region'][$v2['id']]['pid']=$v1['id'];
                    $newArray[$v['id']]['city'][$v1['id']]['region'][$v2['id']]['level']=3;
                }
            }
        }
        return json(['code'=>'success','data'=>$newArray]);
    }
    /**
     * 添加人员
     */
    public function add(){
        if(!$this->request->isAjax()) {
            //获取所有城市管理员
            $roleList=Db::name('love_area')->where(['pid'=>0,'status'=>1])->field('id,name,pid')->select()->toArray();
            //获取城市,区域,楼盘三级联动
            $newArray=[];
            foreach ($roleList as $k=>$v){
                $city=Db::name('love_area')->where(['pid'=>$v['id']])->field('id,name,pid')->select()->toArray();
                $newArray[$v['id']]=$v;
                $newArray[$v['id']]['level']=1;
                foreach ($city as $k1=>$v1){
                    $newArray[$v['id']]['city'][$v1['id']]=$v1;
                    $newArray[$v['id']]['city'][$v1['id']]['level']=2;
                    $newArray[$v['id']]['city'][$v1['id']]['region']=[];
                    $region=Db::name('love_building')->where(['areaId'=>$v1['id'],'guanlisel'=>0])->field('id,name')->select()->toArray();
                    foreach ($region as $k2=>$v2){
                        $newArray[$v['id']]['city'][$v1['id']]['region'][$v2['id']]=$v2;
                        $newArray[$v['id']]['city'][$v1['id']]['region'][$v2['id']]['pid']=$v1['id'];
                        $newArray[$v['id']]['city'][$v1['id']]['region'][$v2['id']]['level']=3;
                    }
                }
            }
            return $this->fetch('add', compact('newArray','roleList'));
        }
        //新增记录
        //var_dump($this->postData('agent'));
        $agentdata=$this->postData('agent');
        //var_dump($agentdata);die;
        //账号名称去重
        $has=Db::name('love_admin_user')->where(['user_name'=>$agentdata['user_name']])->field('id')->find();
        if($has){
            return $this->renderError($this->model->getError() ?: '该账号已存在');
        }
        //判断密码和确认密码是否一致
        if($agentdata['password']!=$agentdata['password_confirm']){
            return $this->renderError($this->model->getError() ?: '两次密码不一致');
        }
        if($agentdata['type']==2){
            //城市管理员
            //判断该城市是否已添加过管理员
            $is=Db::name('love_admin_user')->where(['cityid'=>$agentdata['cityId2'],'areaid'=>'all'])->field('id')->find();
            if($is){
                return $this->renderError($this->model->getError() ?: '该城市已添加过管理员');
            }
            $data['login_type']=1;
            $data['pid']=1;
            $data['cityid']=$agentdata['cityId2'];
            $data['areaid']='all';
            $data['loupanids']='';
        }elseif ($agentdata['type']==3){
            //区域管理员
            //判断该区域是否已添加上级管理员
            $is2=Db::name('love_admin_user')->where(['cityid'=>$agentdata['cityId1'],'type'=>2])->field('id')->find();
            if(empty($is2)){
                return $this->renderError($this->model->getError() ?: '请先添加城市管理员');
            }
            //判断该区域是否已添加过管理员
            $is1=Db::name('love_admin_user')->where(['cityid'=>$agentdata['cityId1'],'areaid'=>$agentdata['areaId1'],'type'=>3])->field('id')->find();
            if($is1){
                return $this->renderError($this->model->getError() ?: '该区域已添加过管理员');
            }
            //获取pid
            $pid=Db::name('love_admin_user')->where(['cityid'=>$agentdata['cityId1'],'areaid'=>'all','type'=>2])->field('id')->find();
            $data['pid']=$pid['id'];
            $data['login_type']=1;
            $data['cityid']=$agentdata['cityId1'];
            $data['areaid']=$agentdata['areaId1'];
            $data['loupanids']='';
        }elseif ($agentdata['type']==4){
            //销售管理员
            //判断该区域是否已添加上级管理员 获取pid
            $pid1=Db::name('love_admin_user')->where(['cityid'=>$agentdata['cityId'],'areaid'=>$agentdata['areaId'],'type'=>3])->field('id')->find();
            if(empty($pid1)){
                return $this->renderError($this->model->getError() ?: '请先添加区域管理员');
            }
            //判断该区域是否已添加过管理员
            /*$is11=Db::name('love_admin_user')->where(['cityid'=>$agentdata['cityId'],'areaid'=>$agentdata['areaId'],'type'=>4])->field('id')->find();
            if($is11){
                return $this->renderError($this->model->getError() ?: '该区域已添加过管理员');
            }*/
            if(empty($agentdata['loupanids'])){
                return $this->renderError($this->model->getError() ?: '请选择楼盘');
            }
            $data['pid']=$pid1['id'];
            $data['login_type']=2;
            $data['cityid']=$agentdata['cityId'];
            $data['areaid']=$agentdata['areaId'];
            $data['loupanids']=implode(',',$agentdata['loupanids']);
        }elseif ($agentdata['type']==5){
            //销售顾问
            //判断该销售顾问所选的楼盘是否已添加销售管理员
            /*$pid11=Db::name('love_admin_user')->where(['cityid'=>$agentdata['cityId'],'areaid'=>$agentdata['areaId'],'type'=>4])->field('id')->find();
            if(empty($pid11)){
                return $this->renderError($this->model->getError() ?: '请先添加销售管理员');
            }*/
            if(empty($agentdata['loupanids3'])){
                return $this->renderError($this->model->getError() ?: '请选择楼盘');
            }
            $a=0;
            $fid=0;
            $pid11=Db::name('love_admin_user')->where(['cityid'=>$agentdata['cityId3'],'areaid'=>$agentdata['areaId3'],'type'=>4])->field('id,loupanids')->select()->toArray();
            if(empty($pid11)){
                return $this->renderError($this->model->getError() ?: '请先添加销售管理员');
            }else{
                foreach ($pid11 as $kk=>$vv){
                    $exo=explode(',',$vv['loupanids']);
                    if(in_array($agentdata['loupanids3'],$exo)){
                        $a=1;
                        $fid=$vv['id'];
                    }
                }
                if($a==0){
                    return $this->renderError($this->model->getError() ?: '该楼盘未添加销售管理员');
                }
            }
            $data['pid']=$fid;
            $data['login_type']=2;
            $data['cityid']=$agentdata['cityId3'];
            $data['areaid']=$agentdata['areaId3'];
            $data['loupanids']=$agentdata['loupanids3'];
        }elseif ($agentdata['type']==6){
            //楼管
            //判断该区域是否已添加上级管理员
            $pid3=Db::name('love_admin_user')->where(['cityid'=>$agentdata['cityId1'],'areaid'=>$agentdata['areaId1'],'type'=>3])->field('id')->find();
            if(empty($pid3)){
                return $this->renderError($this->model->getError() ?: '请先添加区域管理员');
            }
            $data['pid']=$pid3['id'];
            $data['login_type']=2;
            $data['cityid']=$agentdata['cityId1'];
            $data['areaid']=$agentdata['areaId1'];
            $data['loupanids']='';
        }else{
            //楼盘管理人员
            //判断该区域是否已添加上级管理员
            $pid3=Db::name('love_admin_user')->where(['cityid'=>$agentdata['cityId1'],'areaid'=>$agentdata['areaId1'],'type'=>3])->field('id')->find();
            if(empty($pid3)){
                return $this->renderError($this->model->getError() ?: '请先添加区域管理员');
            }
            //判断该区域是否已添加过楼管管理人员
            $pid4=Db::name('love_admin_user')->where(['cityid'=>$agentdata['cityId1'],'areaid'=>$agentdata['areaId1'],'type'=>7])->field('id')->find();
            if(!empty($pid4)){
                return $this->renderError($this->model->getError() ?: '该区域已添加过楼管管理人员');
            }
            $data['pid']=$pid3['id'];
            $data['login_type']=1;
            $data['cityid']=$agentdata['cityId1'];
            $data['areaid']=$agentdata['areaId1'];
            $data['loupanids']='';
        }
        $data['name']=$agentdata['name'];
        $data['user_name']=$agentdata['user_name'];
        $data['password']=md5($agentdata['password']);
        $data['create_time']=time();
        $data['update_time']=0;
        $data['is_super']=1;
        $data['type']=$agentdata['type'];
        if($agentdata['type']==4){
            //更新楼盘状态
            $update=Db::name('love_building')->where(['id'=>['in',$data['loupanids']]])->update(['guanlisel'=>1]);
        }
        $insert=Db::name('love_admin_user')->insertGetId($data);
        if($insert) {
            return $this->renderSuccess('添加成功', url('agent.agent/index'));
        }
        return $this->renderError($this->model->getError() ?: '添加失败');
    }
    /**
     * 编辑人员信息
     */
    public function edit($user_id){

        if(!$this->request->isAjax()){
            $info=Db::name('love_admin_user')->where(['id'=>$user_id])->find();
            return $this->fetch('edit', compact('info'));
        }
        $data=$this->postData('agent');
        if(!empty($data['password'])){
            if(empty($data['password_confirm'])){
                return $this->renderError('请输入确认密码');
            }
            if($data['password']!=$data['password_confirm']){
                return $this->renderError('两次输入的密码不一致');
            }
            $datas['name']=$data['name'];
            $datas['user_name']=$data['user_name'];
            $datas['password']=md5($data['password']);

        }else{
            $datas['name']=$data['name'];
            $datas['user_name']=$data['user_name'];
        }
        $datas['update_time']=time();
        Db::name('love_admin_user')->where(['id'=>$user_id])->update($datas);
        return $this->renderSuccess('修改成功', url('agent.agent/index'));
    }
    /**
     * 删除
     */
    public function delete($userid){
        $info=Db::name('love_admin_user')->where(['id'=>$userid])->update(['is_delete'=>1,'update_time'=>time()]);
        if (!$info) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功',url('agent.agent/index'));
    }
    /**
     * 恢复
     */
    public function recover($userid){
        $info=Db::name('love_admin_user')->where(['id'=>$userid])->update(['is_delete'=>0,'update_time'=>time()]);
        if(!$info){
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功',url('agent.agent/index'));
    }
}